//
//  VehicleInquiryController.m
//  Runone
//
//  Created by runone02 on 16/10/25.
//  Copyright © 2016年 runone. All rights reserved.
//  车辆信息列表

#import "VehicleInquiryController.h"
#import "VehicleListViewController.h"
#import "RequestGinsengModel.h"
#import "RequestOutginseng.h"
#import "RequestHelper.h"
#import "AppDelegate.h"
#import "InVehicleModel.h"
#import "MJExtension.h"
#import "VehiclePlanDetailViewController.h"
#import "UIColor+Addition.h"
#import "UIView+Addition.h"

@interface VehicleInquiryController () <UIScrollViewDelegate>
// ************************ - 主界面Ui - ************************
@property (nonatomic, strong) UIView *titlesView;
@property (nonatomic, strong)UIView  *indicator;
@property (nonatomic, strong) UIScrollView *mainScrollView; //横向滚动的容器
@property (nonatomic, strong) UIView *lineV;

@property (nonatomic, strong)UIButton *selectedButton;

// ************************ - 子控制器 - ************************
@property (nonatomic, strong) VehicleListViewController *administrativeVC;//行政
@property (nonatomic, strong) VehicleListViewController *roadAdminVC;     //路政
@property (nonatomic, strong) VehicleListViewController *rescueVC;        //救援
@property (nonatomic, strong) VehicleListViewController *conservationVC;  //养护

// ************************ - 车辆数组 - ************************
@property (nonatomic, strong) NSArray *allDataArray;//所有车辆的数组
@property (nonatomic, strong) NSMutableArray *adminArray; //行政车
@property (nonatomic, strong) NSMutableArray *roadArray;//路政车
@property (nonatomic, strong) NSMutableArray *rescueArray;//救援
@property (nonatomic, strong) NSMutableArray *conservationArray; //养护

@end

@implementation VehicleInquiryController

#pragma      ------------  车辆信息列表

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configDataArray];//创建保存数据的字典
    [self configNaviBar];//设置导航栏
    [self requestAllInternalVehicle];// 请求数据
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden  = NO;
}

#pragma mark - 数组
- (void) configDataArray {
    _allDataArray      = [[NSArray alloc] init];
    _adminArray        = [[NSMutableArray alloc] init];
    _roadArray         = [[NSMutableArray alloc] init];
    _rescueArray       = [[NSMutableArray alloc] init];
    _conservationArray = [[NSMutableArray alloc] init];
}

#pragma mark - 设置导航栏
- (void) configNaviBar {
    UILabel *titleLabel           = [[UILabel alloc] init];
    titleLabel.size               = CGSizeMake(100, 30);
    titleLabel.text               = @"车辆信息";
    titleLabel.font               = [UIFont systemFontOfSize:20];
    titleLabel.textAlignment      = NSTextAlignmentCenter;
    titleLabel.textColor          = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    self.view.backgroundColor     = [UIColor whiteColor];
    UIButton *btnPlan             = [UIButton buttonWithType:UIButtonTypeCustom];
    btnPlan.titleLabel.font       = [UIFont systemFontOfSize:17];
    [btnPlan setTitle:@"用车计划" forState:UIControlStateNormal];
    [btnPlan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnPlan sizeToFit];
    [btnPlan addTarget:self action:@selector(btnPlanClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem             = [[UIBarButtonItem alloc] initWithCustomView:btnPlan];
    self.navigationItem.rightBarButtonItem = rightItem;
}
#pragma mark - 用车计划
- (void) btnPlanClick {
    VehiclePlanDetailViewController *vc = [[VehiclePlanDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 子控制器
- (void) setChildViewConrtollers {
    VehicleListViewController *administrativeVC = [[VehicleListViewController alloc]init];
    administrativeVC.title   = @"行政";
    administrativeVC.dataArr = _adminArray;
    [self addChildViewController:administrativeVC];
    _administrativeVC = administrativeVC;
    
    VehicleListViewController *roadAdminVC = [[VehicleListViewController alloc]init];
    roadAdminVC.title = @"路政";
    roadAdminVC.dataArr = _roadArray;
    [self addChildViewController:roadAdminVC];
    _roadAdminVC = roadAdminVC;
    
    VehicleListViewController *rescueVC = [[VehicleListViewController alloc]init];
    rescueVC.title = @"救援";
    rescueVC.dataArr = _rescueArray;
    [self addChildViewController:rescueVC];
    _rescueVC = rescueVC;
    
    VehicleListViewController *conservationVC = [[VehicleListViewController alloc] init];
    conservationVC.title = @"养护";
    conservationVC.dataArr = _conservationArray;
    [self addChildViewController:conservationVC];
    _conservationVC = conservationVC;
}

#pragma mark - 头部
- (void) configUI {
    _titlesView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.width ,44)];
    _titlesView.tag = 200;
    _titlesView.backgroundColor = [UIColor whiteColor];
//    self.titlesView = _titlesView;
    [self.view addSubview:_titlesView];
    //    [self.view exchangeSubviewAtIndex:100 withSubviewAtIndex:101];
    _indicator = [[UIView alloc]init];
    _indicator.height = 2;
    _indicator.width  = self.view.size.width / 4;
    _indicator.y = _titlesView.height - _indicator.height;
    _indicator.backgroundColor = [UIColor colorWithRed:54.0 / 255 green:105.0 / 255 blue:145.0 / 255 alpha:1];
    //横线
    _lineV        = [[UIView alloc] init];
    _lineV.height = 1;
    _lineV.y      = _titlesView.height;
    _lineV.x      =  0;
    _lineV.width  = self.view.width ;
    _lineV.backgroundColor = [UIColor lightGrayColor];
    
    NSInteger count  = self.childViewControllers.count;
    CGFloat width    = self.view.width / count ;
    CGFloat height   = _titlesView.height;
    for (NSUInteger i = 0; i < count; i++) {
        UIButton *tagBtn = [[UIButton alloc]init];
        tagBtn.tag       = i ;
        tagBtn.width     = width;
        tagBtn.height    = height - 2;
        tagBtn.x         = i * width;
        VehicleListViewController *vc = self.childViewControllers[i];
        [tagBtn setTitle:vc.title forState:UIControlStateNormal];
        tagBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [tagBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [tagBtn setTitleColor:[UIColor colorWithRed:54.0 / 255 green:176.0 / 255 blue:219.0 / 255 alpha:1] forState:UIControlStateDisabled];
        [tagBtn layoutIfNeeded];//让titleLable有值
        [tagBtn addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside];
        [_titlesView addSubview:tagBtn];
        if ( i == 0) {
            [self tagClick:tagBtn];
        }
    }
    [_titlesView addSubview:_indicator];
    [_titlesView addSubview: _lineV];
}
-(void)configcontent {
    self.automaticallyAdjustsScrollViewInsets = NO;
    _mainScrollView               = [[UIScrollView alloc]init];
    _mainScrollView.frame         = self.view.bounds;
    _mainScrollView.delegate      = self;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.contentSize   = CGSizeMake(self.childViewControllers.count * _mainScrollView.width, 0);
    [self.view insertSubview:_mainScrollView atIndex:0];
    [self scrollViewDidEndScrollingAnimation:_mainScrollView];
}
# pragma mark - 标题按钮点击
-(void)tagClick:(UIButton *)tagBtn {
    self.selectedButton.enabled = YES;
    tagBtn.enabled              = NO;
    self.selectedButton         = tagBtn;
    [UIView animateWithDuration:0.30 animations:^{
        _indicator.centerX = tagBtn.centerX;
    }];
    CGPoint offset = self.mainScrollView.contentOffset;
    offset.x       = tagBtn.tag * self.view.width;
    [self.mainScrollView setContentOffset:offset animated:YES];
}
#pragma mark - 请求所有的内部车辆信息
- (void) requestAllInternalVehicle {
    RequestHelper *requestHelper = [[RequestHelper alloc] init];
    RequestGinsengModel *getInvcModel = [[RequestGinsengModel alloc] init];
    getInvcModel.URL                  = GetVehicleDataURL;
    getInvcModel.DataValue            = @"";
    getInvcModel.UpdatedTime          = @"2012-01-12 12:00:00";
    getInvcModel.SystemCode           = @"GuangleHighway";
    getInvcModel.MethodName           = @"GetIntVclBaseInfo";
    requestHelper.block = ^(RequestOutginseng *requestOutginseng,id dataValueModel) {
        _allDataArray  = [InVehicleModel mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
        //数据处理 ,分类
        [self classifyInVehivleType];
        [self setChildViewConrtollers];
        [self configUI];
        [self configcontent];
    };
    [requestHelper RequestGinsengModel:getInvcModel andPushController:self];
}
#pragma mark - 分类
- (void) classifyInVehivleType {
    for (InVehicleModel *inModel in _allDataArray) {
        switch (inModel.VehicleType) {
            case 1:
                [_roadArray addObject:inModel];        //路政
                break;
            case 2:
                [_rescueArray addObject:inModel];      //救援
                break;
            case 3:
                [_conservationArray addObject:inModel];//养护
                break;
            case 4:
                break;
            default:
                [_adminArray addObject:inModel];       //行政
                break;
        }
    }
}

#pragma mark - scrollViewdelegate
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / self.view.width;
    VehicleListViewController *oneVc = self.childViewControllers[index];
    oneVc.view.x = scrollView.contentOffset.x;
    oneVc.view.y = 0;
    oneVc.view.height = scrollView.height;
    [scrollView addSubview:oneVc.view];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:_mainScrollView];
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self tagClick:self.titlesView.subviews[index]];
}


@end
